package com.cyandpos.pad.activity;


import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Editable;
import android.text.Selection;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;

import com.changyi.device.APApplication;
import com.cyandpos.pad.R;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.commondata.Urls;
import com.cyandpos.pad.presentation.AdPresentationUtil;
import com.cyandpos.pad.utils.CommonUtils;
import com.cyandpos.pad.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SettingActivity extends AppCompatActivity {

    @BindView(R.id.et_sktno_setting_act)
    EditText etSktno;
    @BindView(R.id.et_fdbh_setting_act)
    EditText etFdbh;
    @BindView(R.id.et_ip_setting_act)
    EditText etIp;
    @BindView(R.id.et_port_setting_act)
    EditText etPort;
    @BindView(R.id.btn_cancel_setting_act)
    Button btnCancel;
    @BindView(R.id.btn_sure_setting_act)
    Button btnSure;

    @BindView(R.id.btn_seven_login_act)
    Button btnSeven;
    @BindView(R.id.btn_eight_login_act)
    Button btnEight;
    @BindView(R.id.btn_nine_login_act)
    Button btnNine;
    @BindView(R.id.btn_four_login_act)
    Button btnFour;
    @BindView(R.id.btn_five_login_act)
    Button btnFive;
    @BindView(R.id.btn_six_login_act)
    Button btnSix;
    @BindView(R.id.btn_one_login_act)
    Button btnOne;
    @BindView(R.id.btn_two_login_act)
    Button btnTwo;
    @BindView(R.id.btn_three_login_act)
    Button btnThree;
    @BindView(R.id.btn_zero_login_act)
    Button btnZero;
    @BindView(R.id.btn_dot_st_act)
    Button btnDot;
    @BindView(R.id.btn_back_st_act)
    Button btnBack;
    @BindView(R.id.et_pass_setting)
    EditText et_pass_setting;
    @BindView(R.id.et_pass_setting_appsercet)
    EditText et_pass_setting_appsercet;
    private static final String FOCUS_FLAG_SKTNO = "etSktno";
    private static final String FOCUS_FLAG_FDBH= "etFdbh";
    private static final String FOCUS_FLAG_IP = "etIP";
    private static final String FOCUS_FLAG_PORT = "etPort";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        APApplication.getInstance().addActivity(this);
        new AdPresentationUtil().initDisplayData(this);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        CommonUtils.setNoSoftware(etSktno,SettingActivity.this,true);
        CommonUtils.setNoSoftware(etFdbh,SettingActivity.this,true);
        CommonUtils.setNoSoftware(etIp,SettingActivity.this,true);
//        CommonUtils.setNoSoftware(etPort,SettingActivity.this,true);

        btnZero.setOnClickListener(new CalOnClickListener());
        btnOne.setOnClickListener(new CalOnClickListener());
        btnTwo.setOnClickListener(new CalOnClickListener());
        btnThree.setOnClickListener(new CalOnClickListener());
        btnFour.setOnClickListener(new CalOnClickListener());
        btnFive.setOnClickListener(new CalOnClickListener());
        btnSix.setOnClickListener(new CalOnClickListener());
        btnSeven.setOnClickListener(new CalOnClickListener());
        btnEight.setOnClickListener(new CalOnClickListener());
        btnNine.setOnClickListener(new CalOnClickListener());
        btnDot.setOnClickListener(new CalOnClickListener());
        btnBack.setOnClickListener(new CalOnClickListener());
        btnSure.setOnClickListener(new CalOnClickListener());
        btnCancel.setOnClickListener(new CalOnClickListener());
        btnBack.setOnLongClickListener(new BackOnLongClickListener());

        readSettingInfoLocal();
        setOnFocusStatus(etSktno,etSktno.getText().toString().trim());
    }
//    public void setNoSoftware(EditText et) {
//        CommonUtils.hideSoftInputButWithSelection(et, SettingActivity.this);
//        et.setInputType(EditorInfo.TYPE_CLASS_TEXT);
//        et.setImeOptions(EditorInfo.IME_ACTION_UNSPECIFIED);
//    }
    private void readSettingInfoLocal() {
        SharedPreferences sp = getSharedPreferences("LOGIN", 0);
        String localIp = sp.getString("IP", "");
        String localPort = sp.getString("PORT", "8006/BFPOSQZJ");
        String localSktno = sp.getString("SKTNO","100001");
        String localFdbh = sp.getString("STORECODE","1");
        String localPass = sp.getString("KEY","bfv10pos");
        String localSercet = sp.getString("SERCET","bfv10pos");
//        String localIp = sp.getString("IP", "39.98.242.166");
//        String localPort = sp.getString("PORT", "8006/BFPOSQZJ");
//        String localSktno = sp.getString("SKTNO","100001");
//        String localFdbh = sp.getString("STORECODE","1");
        et_pass_setting.setText(localPass);
        et_pass_setting_appsercet.setText(localSercet);
        etIp.setText(localIp);
        etPort.setText(localPort);
        etSktno.setText(localSktno);
        etFdbh.setText(localFdbh);
        Urls.posAddress = localIp + ":" + localPort;
    }
    class BackOnLongClickListener implements View.OnLongClickListener {

        @Override
        public boolean onLongClick(View view) {
            if (view.getId() == R.id.btn_back_st_act) {
                calLongBack();
            }
            return false;
        }
    }
    private void calLongBack() {
        String temp = getEditFocus();
        if (temp.equals(FOCUS_FLAG_SKTNO)) {
            if (!etSktno.getText().toString().equals("")) {
                etSktno.setText("");
            }
        }else if(temp.equals(FOCUS_FLAG_FDBH)){
            if(!etFdbh.getText().toString().equals("")){
                etFdbh.setText("");
            }
        }
        else if (temp.equals(FOCUS_FLAG_IP)) {
            if (!etIp.getText().toString().equals("")) {
                etIp.setText("");
            }
        } else if(temp.equals(FOCUS_FLAG_PORT)){
            if (!etPort.getText().toString().equals("")) {
                etPort.setText("");
            }
        }
    }

    public void setOnFocusStatus(final EditText et, String defaultValue) {
        Editable etext = et.getText();
        Selection.setSelection(etext, etext.length());
        et.setText(defaultValue);
        et.setFocusable(true);
        et.requestFocus();
    }

    class CalOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_zero_login_act:
                    calInput("0");
                    break;
                case R.id.btn_one_login_act:
                    calInput("1");
                    break;
                case R.id.btn_two_login_act:
                    calInput("2");
                    break;
                case R.id.btn_three_login_act:
                    calInput("3");
                    break;
                case R.id.btn_four_login_act:
                    calInput("4");
                    break;
                case R.id.btn_five_login_act:
                    calInput("5");
                    break;
                case R.id.btn_six_login_act:
                    calInput("6");
                    break;
                case R.id.btn_seven_login_act:
                    calInput("7");
                    break;
                case R.id.btn_eight_login_act:
                    calInput("8");
                    break;
                case R.id.btn_nine_login_act:
                    calInput("9");
                    break;
                case R.id.btn_dot_st_act:
                    calInput(".");
                    break;
                case R.id.btn_back_st_act:
                    calBack();
                    break;
                case R.id.btn_cancel_setting_act://取消
                    finish();
                    break;
                case R.id.btn_sure_setting_act: //确认
                    if(CommonUtils.getContent(etSktno).equals("")){
                        ToastUtils.showMsg("收款台不能为空");
                        return;
                    }
                    if(CommonUtils.getContent(etFdbh).equals("")){
                        ToastUtils.showMsg("分店编号不能为空");
                        return;
                    }
                    if(!CommonUtils.isIp(CommonUtils.getContent(etIp))){
                        ToastUtils.showMsg("错误的服务地址");
                        return;
                    }
                    saveSettingInfoToLocal();
                    ToastUtils.showMsg("设置成功");
                    finish();
                    break;
                default:
                    break;
            }
        }
    }
    private void calInput(String num) {
        String temp = getEditFocus();
        if (temp.equals(FOCUS_FLAG_SKTNO)) {
//            etSktno.append(num);
            CommonUtils.insertEditTextContext(etSktno,num);
        }else if(temp.equals(FOCUS_FLAG_FDBH)){
//            etFdbh.append(num);
            CommonUtils.insertEditTextContext(etFdbh,num);
        } else if (temp.equals(FOCUS_FLAG_IP)) {
//            etIp.append(num);
            CommonUtils.insertEditTextContext(etIp,num);
        } else if(temp.equals(FOCUS_FLAG_PORT)){
//            etPort.append(num);
            CommonUtils.insertEditTextContext(etPort,num);
        }
    }


    private String getEditFocus() {
        if (etSktno.hasFocus()) {
            return FOCUS_FLAG_SKTNO;
        }else if(etFdbh.hasFocus()){
            return FOCUS_FLAG_FDBH;
        } else if (etIp.hasFocus()) {
            return FOCUS_FLAG_IP;
        } else if(etPort.hasFocus()){
            return FOCUS_FLAG_PORT;
        }else {
            return null;
        }
    }

    private void calBack() {
        String temp = getEditFocus();
        if (temp.equals(FOCUS_FLAG_SKTNO)) {
            CommonUtils.delectEditTextContext(etSktno);
        }else if(temp.equals(FOCUS_FLAG_FDBH)){
            CommonUtils.delectEditTextContext(etFdbh);
        }
        else if (temp.equals(FOCUS_FLAG_IP)) {
            CommonUtils.delectEditTextContext(etIp);

        } else if (temp.equals(FOCUS_FLAG_PORT)){
            CommonUtils.delectEditTextContext(etPort);
        }
    }

    private void saveSettingInfoToLocal() {
        SharedPreferences sp = getSharedPreferences("LOGIN", 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("SKTNO",CommonUtils.getContent(etSktno));
        editor.putString("STORECODE",CommonUtils.getContent(etFdbh));
        editor.putString("IP", CommonUtils.getContent(etIp));
        editor.putString("PORT", CommonUtils.getContent(etPort));
        editor.putString("KEY", CommonUtils.getContent(et_pass_setting));
        editor.putString("SERCET", CommonUtils.getContent(et_pass_setting_appsercet));
        editor.commit();
        String sktno = sp.getString("SKTNO","");
        String fdbh = sp.getString("STORECODE","");
        ErpCommonData.sktno = sktno;
        ErpCommonData.storeCode = fdbh;
        Urls.posAddress = CommonUtils.getContent(etIp) + ":" + CommonUtils.getContent(etPort);
    }

}
